<?php 
App::uses('Shell', 'Console');

/**
 * Application Shell
 *
 * Add your application-wide methods in the class below, your shells
 * will inherit them.
 *
 * @package       app.Console.Command
 */
class GeonamesShell extends AppShell {
	
	var $langs = array('en', 'pt', 'fr', 'es');
	var $user = 'gwdweb';
	var $urlBase = 'http://api.geonames.org/';
	var $data;
	var $uses = array('Geonames.Pais', 'Geonames.RegiaoAdministrativa', 'Geonames.Cidade');
	var $begin;
	
	public function main(){
		$begin = time();
		//$this->lista_paises();
		$this->out(time()-$begin);
		$this->begin = time();
		$this->lista_cidades();
	}
	
	private function doSearch($method, $lang, $data = null){
		$url = $this->makeUrl($method, $lang, $data);
		$xml = file_get_contents($url);
		$data = new SimpleXMLElement($xml);
		return $data;
	}
	
	private function makeUrl($method, $lang, $data = null){
		$url = $this->urlBase.$method.'?'.'username='.$this->user;
		$url .= '&type=xml';
		$url .= '&lang='.$lang;
		if($data != null){
			foreach($data as $key=>$item){
				$url .= '&'.$key.'='.$item;
			}
		}
		$this->out($url);
		return $url;
	}
	
	public function lista_paises() {
		$method = 'countryInfo';
		foreach($this->langs as $lang){
			$this->out($lang);
			$lista = $this->doSearch($method, $lang);
			foreach($lista as $item){
				$toSave = array(
					'geonameId'=>$item->geonameId,
					'countryCode'=>$item->countryCode,
					'isoAlpha3'=>$item->isoAlpha3,
					'countryName'=>$item->countryName,
					'language'=>$lang
				);
				if($pais = $this->Pais->find('first', array('conditions'=>array('geonameId'=>$toSave['geonameId'], 'language'=>$lang))))
					$this->Pais->id = $pais['Pais']['id'];
				else
					$this->Pais->create();
				$this->Pais->save($toSave);
			}
		}
		
		return $this->data['url'];
	}
	
	public function lista_regioes_administrativas(){
		$method = 'children';
		$paises = $this->Pais->find('list', array('conditions'=>array('language'=>$this->langs[0]), 'fields'=>array('geonameId')));
		foreach($paises as $id=>$geonameId){
			foreach ($this->langs as $lang){
				$this->out($lang);
				$lista = $this->doSearch($method, $lang, array('geonameId'=>$geonameId, 'maxRows'=>100000));
				foreach($lista->geoname as $item){
					$toSave = array(
							'geonameId'=>$item->geonameId,
							'geonameParentId'=>$geonameId,
							'countryCode'=>$item->countryCode,
							'name'=>$item->name,
							'language'=>$lang
					);
					if($regiao = $this->RegiaoAdministrativa->find('first', array('conditions'=>array('geonameId'=>$toSave['geonameId'], 'language'=>$lang))))
						$this->RegiaoAdministrativa->id = $regiao['RegiaoAdministrativa']['id'];
					else
						$this->RegiaoAdministrativa->create();
					$this->RegiaoAdministrativa->save($toSave);
					$this->out(time()-$this->begin);
				}
			}
		}
		
	}
	
	public function lista_cidades(){
		$method = 'children';
		$conditions = array('language'=>$this->langs[0]);
		$last_city = $this->Cidade->find('first', array('updated'=>0, 'order'=>'id ASC'));
		if($last_city)
			$conditions = array_merge($conditions, array('geonameId >='=>$last_city['Cidade']['geonameParentId']));
		else{
			$this->Cidade->updateAll(array('updated' => false));
		}
		$paises = $this->RegiaoAdministrativa->find('list', array('conditions'=>$conditions, 'fields'=>array('geonameId'), 'order'=>'geonameId'));
		foreach($paises as $id=>$geonameId){
			foreach ($this->langs as $lang){
				$this->out($lang);
				$lista = $this->doSearch($method, $lang, array('geonameId'=>$geonameId, 'maxRows'=>100000));
				foreach($lista->geoname as $item){
					$toSave = array(
							'geonameId'=>$item->geonameId,
							'geonameParentId'=>$geonameId,
							'countryCode'=>$item->countryCode,
							'name'=>$item->name,
							'language'=>$lang,
							'fcode'=>$item->fcode,
							'updated'=>true
					);
					if($regiao = $this->Cidade->find('first', array('conditions'=>array('geonameId'=>$toSave['geonameId'], 'language'=>$lang))))
						$this->Cidade->id = $regiao['Cidade']['id'];
					else
						$this->Cidade->create();
					$this->Cidade->save($toSave);
					$this->out(time()-$this->begin);
				}
			}
		}
	
	}
	
}